package com.cv4j.app.slice;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import com.chinasoft_ohos.commontools.Toast.Toast;
import com.cv4j.app.ResourceTable;
import com.cv4j.app.drawer.DrawerPopup;
import com.cv4j.app.drawer.MenuManager;
import com.cv4j.app.utils.ClickUtil;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

/**
 * MainAbilitySlice
 *
 * @since 2021-05-14
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private MenuManager menuManager;
    private Text titleText;
    private boolean isOnKeyBacking = false;
    private BasePopupView menuPopup;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
        findComponentById(ResourceTable.Id_open_menu).setClickedListener(this);
        titleText = (Text) findComponentById(ResourceTable.Id_title_text);
        initData();
    }

    private void initData() {
        Ability ability = getAbility();
        FractionManager fractionAbility = ((FractionAbility) ability).getFractionManager();
        menuManager = MenuManager.getInstance(fractionAbility);
        menuManager.show(MenuManager.MenuType.HOME);
        titleText.setText(MenuManager.MenuType.HOME.getTitle());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_open_menu:
                initDrawerPopup();
                break;
        }
    }


    private void initDrawerPopup() {
        if (ClickUtil.isFastClick()) {
            return;
        }
        DrawerPopup popup = new DrawerPopup(getContext(), menuManager.getCurType());
        popup.setItemClickedListener(type -> {
            replaceFraction(type);
        });
        menuPopup = new XPopup.Builder(getContext())
            .enableDrag(true)
            .asCustom(popup)
            .show();
    }

    private void replaceFraction(MenuManager.MenuType menuType) {
        menuManager.show(menuType);
        titleText.setText(menuType.getTitle());

        if (menuPopup != null) {
            menuPopup.dismiss();
        }
    }

    @Override
    protected void onBackPressed() {
        if (isOnKeyBacking) {
            terminate();
        } else {
            isOnKeyBacking = true;
            showToast("再按一次退出程序");
            new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                @Override
                public void run() {
                    isOnKeyBacking = false;
                }
            }, 2000);
        }
    }

    private void showToast(String msg) {
        Toast.show(msg);
    }
}
